home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / Extension Shell 1.5 / Sample Extensions (1.5) / Bell Test ƒ / ES Handler.c < prev    next >
C/C++ Source or Header  |  1996-04-12  |  8KB  |  338 lines

  1. /*    NAME:
  2.         ES Handler.c
  3.  
  4.     WRITTEN BY:
  5.         Dair Grant
  6.                 
  7.     DESCRIPTION:
  8.         This file contains an ES Handler for use by Extension Shell.
  9.  
  10.     ___________________________________________________________________________
  11. */
  12. //=============================================================================
  13. //        Include files                                                                     
  14. //-----------------------------------------------------------------------------
  15. #include <Resources.h>
  16. #include <Gestalt.h>
  17. #include <Traps.h>
  18. #include "A4Stuff.h"
  19. #include "SetupA4.h"
  20. #include "ES.h"
  21. #include "BT AddrsTable.h"
  22. #include "BT Constants.h"
  23. #include "ES Handler.h"
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. //=============================================================================
  40. //        Private function prototypes                                                                     
  41. //-----------------------------------------------------------------------------
  42. void    main(short theMsg, ESParamBlock *theParamBlock);
  43. void    InitialiseParamBlock(void);
  44. void    InitialiseAddrsTable(void);
  45. void    HandleTheError(void);
  46. void    SetUpIcons(short animDelay, short numIcons, short firstIcon);
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. //=============================================================================
  63. //        Global variables                                                                 
  64. //-----------------------------------------------------------------------------
  65. ESParamBlock    *gTheParamBlock;
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. //=============================================================================
  82. //        main : Entry point to our code resource.                                                                 
  83. //-----------------------------------------------------------------------------
  84. //        Note :    Extension Shell communicates with us via a message constant,
  85. //                and a pointer to a structure it owns. Our job is to fill in
  86. //                the details in that structure, depending on what it wants us
  87. //                to do.
  88. //-----------------------------------------------------------------------------
  89. void main(short theMsg, ESParamBlock *theParamBlock)
  90. {    long            oldA4;
  91.  
  92.  
  93.  
  94.     // Set up A4 so that we can access our globals
  95. #ifndef powerc
  96.     oldA4 = SetCurrentA4();
  97. #endif
  98.     gTheParamBlock = theParamBlock;
  99.  
  100.  
  101.  
  102.     // Case out on what we have to do
  103.     switch(theMsg) {
  104.         case kInitialiseParamBlock:
  105.              InitialiseParamBlock();
  106.              break;
  107.              
  108.         case kInitialiseAddrsTable:
  109.              InitialiseAddrsTable();
  110.              break;
  111.  
  112.         case kHandleError:
  113.              HandleTheError();
  114.              break;
  115.     
  116.         default:
  117.              ;
  118.     }
  119.  
  120.  
  121.     // Restore A4
  122. #ifndef powerc
  123.     SetA4(oldA4);
  124. #endif
  125. }
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141. //=============================================================================
  142. //        InitialiseParamBlock : Initialises the ParamBlock.                                                                 
  143. //-----------------------------------------------------------------------------
  144. //        Note :    We have three tasks to perform.
  145. //                    • Check to see if we can run
  146. //                    • Set up the icons we want to display
  147. //                    • Set up the code we want installed
  148. //-----------------------------------------------------------------------------
  149. void InitialiseParamBlock(void)
  150. {
  151.  
  152.  
  153.     // Check for System 7. We depend on having System 7, and won't
  154.     // run if we don't have it. If we don't have it, we beep, post
  155.     // an error message, and show our disabled icon(s).
  156.     if (gTheParamBlock->systemVersion < 0x0700)
  157.         {
  158.         // Error details
  159.         gTheParamBlock->beepNow                = true;
  160.         gTheParamBlock->postError            = true;
  161.         gTheParamBlock->errorStringsID        = kErrorStrings;
  162.         gTheParamBlock->errorStringIndex    = kNeedSystemSeven;
  163.  
  164.  
  165.         // Icon details
  166.         SetUpIcons(kDisabledAnimDelay, kMyNumDisabledIcons, kMyFirstDisabledIcon);
  167.         }
  168.     
  169.     
  170.     
  171.     // If a shift key, or the mouse button, is down, we don't load either.
  172.     // We don't post an error, but we do show our disabled icon(s) to let
  173.     // the user know they've turned us off.
  174.     else if ((*gTheParamBlock->IsKeyMouseDown)(kShiftKey, true))
  175.         {
  176.         // Icon details
  177.         SetUpIcons(kDisabledAnimDelay, kMyNumDisabledIcons, kMyFirstDisabledIcon);
  178.         }
  179.     
  180.     
  181.     
  182.     // Otherwise, we're allowed to run. If we had a Control Panel, we
  183.     // would also check to see if our Control Panel had turned us
  184.     // off (by setting some preference resource).
  185.     else
  186.         {
  187.         // Icon details
  188.         SetUpIcons(kEnabledAnimDelay, kMyNumEnabledIcons, kMyFirstEnabledIcon);
  189.         
  190.         
  191.         // We install one trap patch, one code block, and request an address table
  192.         gTheParamBlock->installAddressTable        = true;
  193.         gTheParamBlock->addressTableSelector    = kBellTestAddressTable;
  194.         gTheParamBlock->numCodeResources        = 2;
  195.  
  196.  
  197.         // Details for a trap patch to MenuSelect()
  198.         gTheParamBlock->theCodeResources[kMenuSelect].resType    = kMenuSelectResType;
  199.         gTheParamBlock->theCodeResources[kMenuSelect].resID        = kMenuSelectResID;
  200.         gTheParamBlock->theCodeResources[kMenuSelect].codeType    = kTrapPatchType;
  201.         gTheParamBlock->theCodeResources[kMenuSelect].theCodeThing.theTrapPatch.trapWord = _MenuSelect;
  202.         gTheParamBlock->theCodeResources[kMenuSelect].theCodeThing.theTrapPatch.globalpatch = true;
  203.         
  204.  
  205.         // Details for a code block
  206.         gTheParamBlock->theCodeResources[kPlaySound].resType    = kPlaySoundResType;
  207.         gTheParamBlock->theCodeResources[kPlaySound].resID        = kPlaySoundResID;
  208.         gTheParamBlock->theCodeResources[kPlaySound].codeType    = kCodeBlockType;
  209.         gTheParamBlock->theCodeResources[kPlaySound].theCodeThing.theCodeBlock.refCon = 0x00000000;
  210.         }
  211. }
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227. //=============================================================================
  228. //        InitialiseAddrsTable : Initialise the address table.                                                     
  229. //-----------------------------------------------------------------------------
  230. //        Note :    If we requested an address table, Extension Shell calls us back
  231. //                to allow us to initialise any extensions we've made to it.
  232. //
  233. //                This routine will only be called if we request an address
  234. //                table, and is called after the address table is installed,
  235. //                but before any of the items in gTheParamBlock->theCodeResources
  236. //                are processed (since they might need to access the address
  237. //                table).
  238. //
  239. //                We should initialise the magicNumber and versionNumber fields
  240. //                with constants fixed for this build. One possible number for the
  241. //                magicNumber field would be sizeof() our address table structure.
  242. //-----------------------------------------------------------------------------
  243. void InitialiseAddrsTable(void)
  244. {    BTAddressTable    *theAddressTable;
  245.     Handle            theHnd;
  246.  
  247.  
  248.  
  249.     // Call Gestalt to find our address table
  250.     Gestalt(kBellTestAddressTable, (long *) &theAddressTable);
  251.  
  252.  
  253.     // Initialise the magic number and version fields
  254.     theAddressTable->magicNumber    = kMyTableMagic;
  255.     theAddressTable->versionNumber    = kMyTableVersion;
  256.  
  257.  
  258.  
  259.     // Initialise our custom values
  260.     theHnd = GetResource('snd ', kSoundToPlay);
  261.     DetachResource(theHnd);
  262.     theAddressTable->theSound = theHnd;
  263. }
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279. //=============================================================================
  280. //        HandleTheError : Handle any errors                                                             
  281. //-----------------------------------------------------------------------------
  282. //        Note :    This routine is called if an error occurred during the
  283. //                installation of the items in gTheParamBlock->theCodeResources.
  284. //
  285. //                If an error occurs we beep, post an error, and request that
  286. //                as much as possible of our code be uninstalled. We also reset
  287. //                the icon details to show our disabled icons.
  288. //-----------------------------------------------------------------------------
  289. void HandleTheError(void)
  290. {
  291.  
  292.  
  293.     // Decide how we want to handle the error
  294.     gTheParamBlock->removeInstalledCode    = true;
  295.     gTheParamBlock->beepNow                = true;
  296.     gTheParamBlock->postError            = true;
  297.     gTheParamBlock->errorStringsID        = kErrorStrings;
  298.  
  299.  
  300.     // Message to display to the user
  301.     gTheParamBlock->errorStringIndex = kUnknownError;
  302.  
  303.  
  304.     // Icon details
  305.     SetUpIcons(kDisabledAnimDelay, kMyNumDisabledIcons, kMyFirstDisabledIcon);
  306. }
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322. //=============================================================================
  323. //        SetUpIcons : Set up our the icon fields in gTheParamBlock.                                                         
  324. //-----------------------------------------------------------------------------
  325. //        Note :    We are passed in the resource ID of the first icon, the number
  326. //                of icons, and a delay for animation. We fill these details
  327. //                in to gTheParamBlock.
  328. //-----------------------------------------------------------------------------
  329. void SetUpIcons(short animDelay, short numIcons, short firstIcon)
  330. {    short        i;
  331.  
  332.  
  333.     gTheParamBlock->animationDelay    = animDelay;
  334.     gTheParamBlock->numIcons        = numIcons;
  335.     for (i = 1; i <= numIcons; i++)
  336.         gTheParamBlock->theIcons[i] = firstIcon + i - 1;
  337. }
  338.